<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class Article_categoryController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/article_category/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function index(){

        $list = BackstageModel::getCateList();
        // dd($list);exit;
        return view('backstage/article_category/index',['list'=>$list]);
    }
    public function add(){
        $cate = BackstageModel::getCate('article_category',['pid'=>0 ]);
        return view('backstage/article_category/add',['cate'=>$cate]);
    }
    public function doadd(Request $request){
        if($request->isMethod('POST')){
            
            $data = $request->all();
            // $file = $request->file('art_img');
            // dd($data);exit;
            // 文件是否上傳成功
            // if($file!=null){
            //     if ($file->isValid()) {
            //         // 獲取文件相關信息
            //         $originalName = $file->getClientOriginalName(); // 文件原名
            //         $ext = $file->getClientOriginalExtension();     // 擴展名
            //         $realPath = $file->getRealPath();   //臨時文件的絕對路徑
            //         $type = $file->getClientMimeType();     // image/jpeg
            //         // 上傳文件
            //         $filename = date('YmdHis') . '-' . uniqid() . '.' . $ext;
            //         $article['art_img'] = '/uploads/'.$filename;
            //         // 使用我們新建的uploads本地存儲空間（目錄）
            //         $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
            //         if($bool!=true){
            //             $arrJson = array('status' => 0, 'message'=>'圖片上傳失敗');
            //             return json_encode($arrJson);
            //         }
            //     }
            // }
            
            $cate['pid'] = $data['pid'];
            $cate['type_name'] = $data['type_name'];
            $cate['type_nid'] = $data['type_nid'];

            $cate['type_set'] = $data['type_set'];
            $cate['type_url'] = $data['type_url'];
            $cate['is_blank'] = $data['is_blank'];
            $cate['sort_order'] = empty($data['sort_order']) ? 0 : intval($data['sort_order']);
            $cate['is_hidden'] = $data['is_hidden'];
            $cate['add_time'] = time();
            $newid = CommonModel::addDataGetId('article_category',$cate);
            if($newid == true){
                $arrJson = array('status' => 1, 'message'=>'分類添加成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'分類添加失敗');
            }
            return json_encode($arrJson);
        }
    }
    public function edit($id){
        $table = 'article_category';
        $data =['id'=>$id];
        $art_cate = CommonModel::getOne($table,$data);
        $cate = BackstageModel::getCate('article_category',['pid'=>0 ]);
        return view('backstage/article_category/edit',['cate'=>$cate,'art_cate'=>$art_cate]);
    }
    public function doedit(Request $request){
        if($request->isMethod('POST')){
            
            $data = $request->all();
            // dd($data);exit;
            // 文件是否上傳成功
            $id = $data['id'];
            $cate['pid'] = $data['pid'];
            $cate['type_name'] = $data['type_name'];
            $cate['type_nid'] = $data['type_nid'];

            $cate['type_set'] = $data['type_set'];
            $cate['type_url'] = $data['type_url'];
            $cate['is_blank'] = $data['is_blank'];
            $cate['sort_order'] = $data['sort_order'];
            $cate['is_hidden'] = $data['is_hidden'];
            $cate['add_time'] = time();
            $save = CommonModel::updateData('article_category',['id'=>$id],$cate);
            if($save == true){
                CommonModel::updateData('article_category',['id'=>$id],['add_time'=>time() ]);
                $arrJson = array('status' => 1, 'message'=>'分類修改成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'分類修改失敗');
            }
            return json_encode($arrJson);
        }
    }
    


}
